package com.edu.chongdian;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;

public class MapUtil {
    public static final String PN_GAODE_MAP = "com.autonavi.minimap";// 高德地图包名
    /**
     * 检查地图应用是否安装
     */
    public static boolean isGaodeMapInstall(Context context) {
        return isInstallPackage(context, PN_GAODE_MAP);
    }
    private static boolean isInstallPackage(Context context, String packageName) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo info = packageManager.getPackageInfo(packageName, 0);
            if (info != null) {
                return true;
            }
        } catch (Exception e) {
            // loge(e);
        }
        return false;
    }

    /**
     * 打开高德地图导航（https://lbs.amap.com/api/amap-mobile/guide/android/route）
     *
     * @param context   context
     * @param address   终点名称
     * @param latitude  终点纬度
     * @param longitude 终点经度
     */
    public static void openGaodeNavigation(Context context, String address, double latitude, double longitude) {
        if (isGaodeMapInstall(context)) {
            Uri uri = Uri.parse("amapuri://route/plan/?dlat=" + latitude
                    + "&dlon=" + longitude
                    + "&dname=" + address
                    + "&dev=0&t=0");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);
        } else {
            Toast.makeText(context, "未安装高德地图", Toast.LENGTH_LONG).show();
        }
    }
}
